π― Unit 3: Decision Making and Branching
Welcome back, coder! π
This time, weβre learning how to make our C programs think and choose. Yep β like teaching your code how to make decisions.
π€ Introduction to Decision Makingβ
Decision making structures let programs choose different paths depending on conditions.
Think of it like asking: βIf itβs raining, should I take an umbrella? If not, sunglasses!β πβ
C gives us:
- β Simple if
- β if-else
- β Nested if
- β else-if ladder
- β switch-case
πΉ The if Statementβ
Syntaxβ
if (condition) {
// Executes if condition is true
}
Flow:
Condition β True β Run block
β False β Skip block
Exampleβ
#include <stdio.h>
int main() {
int age = 20;
if (age >= 18) {
printf("You are eligible to vote.\n");
}
printf("Thank you!\n");
return 0;
}
π‘ Tip: if doesnβt need an else. It can stand alone.
πΈ The if-else Statementβ
Gives us two paths β one for true, one for false.
if (condition) {
// True path
} else {
// False path
}
Example: Even or Oddβ
- Code
- Sample Run
#include <stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (number % 2 == 0) {
printf("%d is even.\n", number);
} else {
printf("%d is odd.\n", number);
}
return 0;
}
Enter a number: 7
7 is odd.
πΉ Nested if Statementsβ
One if inside another.
if (condition1) {
if (condition2) {
// Both true
} else {
// Only condition1 true
}
} else {
// condition1 false
}
Example: Driving Eligibilityβ
if (age >= 18) {
if (hasLicense == 'y') {
printf("You can drive!\n");
} else {
printf("You need a license.\n");
}
} else {
printf("You must be 18+.\n");
}
πΈ The else-if Ladderβ
Perfect for checking multiple conditions.
if (marks >= 90) grade = 'A';
else if (marks >= 80) grade = 'B';
else if (marks >= 70) grade = 'C';
else grade = 'F';
Example: Grade Calculatorβ
if (marks < 0 || marks > 100) {
printf("Invalid!\n");
} else if (marks >= 90) {
printf("Grade A\n");
} else if (marks >= 80) {
printf("Grade B\n");
} else {
printf("Grade C or below\n");
}
πΉ The switch-case Statementβ
Efficient multi-way branching.
switch (expression) {
case value1: // do something
break;
case value2:
break;
default:
// fallback
}
Rulesβ
- Expression must be int/char
- Case values = constants only
breakprevents fall-throughdefaultis optional but useful
Example: Calculatorβ
switch(op) {
case '+': result = a + b; break;
case '-': result = a - b; break;
case '*': result = a * b; break;
case '/':
if (b != 0) result = a / b;
else printf("Division by 0!\n");
break;
default: printf("Invalid operator\n");
}
βοΈ if-else vs switchβ
| Feature | if-else | switch |
|---|---|---|
| Condition type | Any | int/char only |
| Multiple checks | β Flexible | β Only equality |
| Floating point | β Yes | β No |
| Range checking | β Easy | β Hard |
| Performance | Slower for many checks | Faster for many cases |
| Best for | Complex conditions | Menu-driven apps |
π§ͺ Practical Mini Projectsβ
Leap Year Checkerβ
if (year % 400 == 0) leap = 1;
else if (year % 100 == 0) leap = 0;
else if (year % 4 == 0) leap = 1;
else leap = 0;
Triangle Validatorβ
Checks if sides form a triangle and its type (Equilateral, Isosceles, Scalene).
Student Grade Systemβ
Use if-else ladder for grades + switch for remarks. Perfect combo!
π Quick Quizβ
Which statement is faster for checking 10 fixed integer values?
- if-else ladder
- switch-case β
- Nested if
- None of the above
π Thatβs it for Unit 3!
Now your code can decide, branch, and react like a pro.
Next stop: Unit 4: Loops π